package com.yinshu.teleshield.tkbase.base.entity;

//import io.metaxk.framework.security.core.LoginUser;
//import io.metaxk.framework.security.core.util.SecurityFrameworkUtils;
import cn.dev33.satoken.stp.StpUtil;
import com.yinshu.teleshield.jwt.ContextUserDto;
import com.yinshu.teleshield.utils.ColumnUtil;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 包括id,create_time,creator,updater,update_time字段的表继承的基础实体
 *
 * @author yangjunxiong
 */
@Getter
@Setter
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class TKEntity<T> extends TKSuperEntity<T> {
    private static final long serialVersionUID = 5169873634279173683L;


    public TKEntity() {
    }

    public TKEntity(@NotNull(message = "id不能为空", groups = Update.class) T id,
                    LocalDateTime createTime,
                    Long creator,
                    Long tenantId,
                    LocalDateTime updateTime,
                    Long updater,
                    Integer deleted) {
        super(id, createTime, creator, tenantId, updateTime, updater, deleted);
    }

    public void setSaveDef() {
//        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        String userId = (String) StpUtil.getExtra(ColumnUtil.getFieldName(ContextUserDto::getId));
        String tenantId = (String) StpUtil.getExtra(ColumnUtil.getFieldName(ContextUserDto::getTenantId));
        LocalDateTime now = LocalDateTime.now();
//        if (Objects.nonNull(loginUser)) {
            this.setTenantId(Long.parseLong(tenantId));
            this.setCreator(Long.parseLong(userId));
            this.setUpdater(Long.parseLong(userId));
//        }
        this.setDeleted(0);
        this.setUpdateTime(now);
        this.setCreateTime(now);
    }

    public void setUpdateDef() {
//        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        String userId = (String) StpUtil.getExtra(ColumnUtil.getFieldName(ContextUserDto::getId));
        String tenantId = (String) StpUtil.getExtra(ColumnUtil.getFieldName(ContextUserDto::getTenantId));
        LocalDateTime now = LocalDateTime.now();
//        if (Objects.nonNull(loginUser)) {
            this.setTenantId(Long.parseLong(tenantId));
            this.setUpdater(Long.parseLong(userId));
//        }
        this.setUpdateTime(now);
    }
}
